//
//  BusinessWhsWhsItemListViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 16/1/19.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "reasonTypeViewController.h"

#import "HAODEsingleton.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "WhsCell.h"
#import "reasonTypeEditViewController.h"
#import "BusinessWhsEditWhsViewController.h"
#import "inputHelper.h"
@interface reasonTypeViewController ()<UISearchBarDelegate,UITextFieldDelegate>

@end

@implementation reasonTypeViewController

{
    UISearchBar *search;
    HAODEsingleton* commdity;
    NSMutableArray*code;
    NSMutableArray*content;
    NSMutableArray*isDefault;
    UITextField*searchTextField;
    bool bol;
    UILabel*noLabel;
}

@synthesize alt = alt;


-(void)viewDidLoad{
    
    [super viewDidLoad];
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.xuanzhong=nil;
    bol=YES;
       UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    self.title=self.titlename;
    //    UIButton*tianjiaBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60/2, 60/2)];
    //    [tianjiaBtn setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
    //    [tianjiaBtn addTarget:self action:@selector(addProduct:) forControlEvents:UIControlEventTouchUpInside];
    //    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:tianjiaBtn];
    //    self.navigationItem.rightBarButtonItem = myBtnBtn;
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    _table.delegate=self;
    _table.dataSource=self;
    
    _table.frame=CGRectMake(0,0, ScreenWidth, ScreenHeight-63);
    commdity=[HAODEsingleton shared];
    _table.backgroundColor= [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    //     [inputHelper setupInputHelperForView:_table withDismissType:InputHelperDismissTypeTapGusture];
    _table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self add];
}

-(void)add{
    code=[[NSMutableArray alloc]init];
    content=[[NSMutableArray alloc]init];
    isDefault=[[NSMutableArray alloc]init];
    [noLabel removeFromSuperview];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param1 = [NSMutableDictionary new];
    [param1 setValue:Token forKey:@"tokenId"];
    [param1 setValue:self.optionName forKey:@"optionName"];
    
         NSLog(@"aaaaaaaa%@",param1);
    [HttpRequest requestData:@"System/AppCodes" params:param1 success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                
                NSLog(@"aaaaaaaa%@",severDataDict.dataDict);
                
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                if (arrr.count==0) {
                    [noLabel removeFromSuperview];
                    noLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,ScreenHeight/2-50, ScreenWidth, 17)];
                    noLabel.text=@"无数据";
                    noLabel.textColor=[UIColor blackColor];
                    noLabel.textAlignment=NSTextAlignmentCenter;
                    
                    [self.view addSubview:noLabel];
                    
                    
                }else{
                    for (NSDictionary* dicc in arrr )
                    {
                        [content addObject:[dicc valueForKey:@"content"]];
                        
                        
                        
                        [code addObject:[dicc valueForKey:@"code"]];
                        
                    }
                }
                [_table reloadData];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            NSLog(@"aaaaaaaa%@",severDataDict.dataDict);
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
    
    
}


-(void)addProduct:(UIButton*)sender{
    //    commdity.how=@"Detail";
    
    reasonTypeEditViewController *detailViewController = [[reasonTypeEditViewController alloc] init];
    detailViewController.titleName=@"添加理由";
    detailViewController.s=@"Add";
    detailViewController.optionName=self.optionName;
    [self.navigationController pushViewController:detailViewController animated:YES];
    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [content count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    WhsCell* cell = (WhsCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"WhsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    
    
    cell.code=[code objectAtIndex:indexPath.row];
    cell.textfield .frame=CGRectMake(15,0,ScreenWidth-70,128/2);
    [cell.textfield setBorderStyle:   UITextBorderStyleNone]; //外框类型
    //      searchTextField.placeholder = @"  会员/手机号/姓名"; //默认显示的字
    cell.textfield.font = [UIFont systemFontOfSize: 16];
    cell.textfield.tag=indexPath.row;
    cell.textfield.returnKeyType = UIReturnKeyDone;
    cell.textfield.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    cell.textfield.delegate = self;
    cell.textfield.tintColor =tintCol;
    cell.textfield.text = [content objectAtIndex:indexPath.row] ;
    cell.textfield.textColor=[UIColor blackColor];
    cell.textfield.userInteractionEnabled=NO;
    cell.bianji.frame=CGRectMake(ScreenWidth-20, 0,30, 55);
    cell.bianji.tag=indexPath.row;
    [cell.bianji setTitle:@"  " forState:UIControlStateNormal];
    [cell.bianji setTitleColor:shenhuicolor forState:UIControlStateNormal];
    cell.bianji.titleLabel.font=[UIFont systemFontOfSize:15];
    
    cell.saveAccount=YES;
    
    //        [cell.bianji addTarget:self action:@selector(bianji:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    if (content.count !=indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(15, 128/2-0.35, ScreenWidth-30, 0.35)];
        lineLabel1.backgroundColor=qianhuicolor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    
    if (content.count ==indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 128/2-0.35, ScreenWidth, 0.35)];
        lineLabel1.backgroundColor=qianhuicolor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    
    
    //    }
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 128/2;
}







-(void)bianji:(UIButton *)sender{
    
    
    
    
    
    
    
    //    reasonTypeEditViewController *detailViewController = [[reasonTypeEditViewController alloc] init];
    //    detailViewController.titleName=@"修改";
    //    detailViewController.sat=@"U";
    //    detailViewController.s=@"Edit";
    //    detailViewController.optionName=self.optionName;
    //    commdity.code=[code objectAtIndex:sender.tag];
    //    commdity.content=[content objectAtIndex:sender.tag];
    //
    //    [self.navigationController pushViewController:detailViewController animated:YES];
    
    
}
#pragma mark - Table view delegate

//
//-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return  UITableViewCellEditingStyleDelete;
//
//}
//- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
//    return YES;
//}
//
//
//-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//
//{
//
//    return @"删除";
//
//}
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//
//
//    self.codestring=[code objectAtIndex:indexPath.row];
//
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    NSString* Token = [user objectForKey:@"tokenId"];
//    NSMutableDictionary *params = [NSMutableDictionary new];
//
//    [params setValue:Token forKey:@"tokenId"];
//    [params setValue:@"D" forKey:@"editType"];
//    [params setValue: self.codestring forKey:@"code"];
//
//    [params setValue:self.optionName forKey:@"optionName"];
//
//    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
//    NSLog(@"33333333====%@",params);
//
//    [HttpRequest requestData:[NSString stringWithFormat:@"Common/AppCode/%@", @"Edit"] params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
//        if (isSuccess) {
//
//            NSLog(@"33333333====%@",severDataDict.dataDict);
//            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
//                [Global hideProgressHUDInternal:self.view];
//                [self add];
//
//
//
//            }else{
//                [Global hideProgressHUDInternal:self.view];
//                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
//                [Global showTextHudSub:requestResultStr view:self.view];
//                [self add];
//            }
//
//        }
//        else
//        {
//            [Global hideProgressHUDInternal:self.view];
//            [self add];
//            [Global showTextHudSub:@"连接失败" view:self.view];
//        }
//
//    }];
//
//
//
//}
//


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    commdity.code=code[indexPath.row];
    commdity.content=content[indexPath.row];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
@end